﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>DotNet-SSE</h1>
    <div id="result"></div>
    <script>
        if ('EventSource' in window) {
            var source = new EventSource('/api/SSE/');

            /* open事件回调函数 */
            source.onopen = function () {
                document.getElementById("result").innerHTML += "SSE通道已建立...<br/>";
            };

            /* message事件回调函数 */
            source.onmessage = function (evt) {
                document.getElementById("result").innerHTML += "Message: " + event.data + "<br/>";
            }

            /* error事件回调函数 */
            source.onerror = function (evt) {
                document.getElementById("result").innerHTML += "SSE通道发生错误<br/>";
            }

            /* SSE_Start事件回调 */
            source.addEventListener('SSE_Start', function (event) {
                document.getElementById("result").innerHTML += "SSE_Start: " + event.data + "<br/>";
            }, false);

            source.addEventListener('eventName', function (event) {
                var data = event.data;
                var origin = event.origin;
                var lastEventId = event.lastEventId;
                // handle message
                console.log(event);
            }, false);
        }
    </script>
</body>
</html>